import { defineStore } from 'pinia';

export interface CommentUser {
    userId: number;
    userName: string;
    userAvatar: string;
}

export interface Reply {
    id: number;
    content: string;
    evaluateTime: string;
    likeNum: number;
    dislikeNum: number;
    isLiked?: boolean;
    isDisliked?: boolean;
    user: CommentUser;
    replyTo?: {
        userId: number;
        userName: string;
    };
}

export interface Comment {
    id: number;
    content: string;
    evaluateTime: string;
    commentNum: number;
    likeNum: number;
    dislikeNum: number;
    isLiked?: boolean;
    isDisliked?: boolean;
    user: CommentUser;
    replies: Reply[];
}

interface CourseEvaluateState {
    comments: Record<number, Comment[]>;
}

export const useCourseEvaluateStore = defineStore({
    id: 'courseEvaluate',
    state: (): CourseEvaluateState => ({
        comments: {}
    }),
    actions: {
        // 添加评论
        addComment(courseId: number, comment: Comment) {
            if (!this.comments[courseId]) {
                this.comments[courseId] = [];
            }
            this.comments[courseId].unshift(comment);
        },
        // 删除评论（包括其所有回复）
        deleteComment(courseId: number, commentId: number) {
            const comments = this.comments[courseId];
            if (comments) {
                this.comments[courseId] = comments.filter(c => c.id !== commentId);
            }
        },
        // 删除回复
        deleteReply(courseId: number, commentId: number, replyId: number) {
            const comments = this.comments[courseId];
            if (comments) {
                const comment = comments.find(c => c.id === commentId);
                if (comment && comment.replies) {
                    comment.replies = comment.replies.filter(r => r.id !== replyId);
                    comment.commentNum = comment.replies.length;
                }
            }
        },
        // 获取课程评论
        getComments(courseId: number): Comment[] {
            return this.comments[courseId] || [];
        },
        // 添加回复
        addReply(courseId: number, commentId: number, reply: Reply) {
            const comments = this.comments[courseId];
            if (comments) {
                const comment = comments.find(c => c.id === commentId);
                if (comment) {
                    if (!comment.replies) {
                        comment.replies = [];
                    }
                    comment.replies.push(reply);
                    comment.commentNum = comment.replies.length;
                }
            }
        },
        // 点赞评论
        likeComment(courseId: number, commentId: number) {
            const comments = this.comments[courseId];
            if (comments) {
                const comment = comments.find(c => c.id === commentId);
                if (comment) {
                    // 如果已经点踩，取消点踩
                    if (comment.isDisliked) {
                        comment.dislikeNum = Math.max(0, comment.dislikeNum - 1);
                        comment.isDisliked = false;
                    }
                    // 切换点赞状态
                    if (!comment.isLiked) {
                        comment.likeNum = (comment.likeNum || 0) + 1;
                        comment.isLiked = true;
                    } else {
                        comment.likeNum = Math.max(0, (comment.likeNum || 0) - 1);
                        comment.isLiked = false;
                    }
                }
            }
        },
        // 点踩评论
        dislikeComment(courseId: number, commentId: number) {
            const comments = this.comments[courseId];
            if (comments) {
                const comment = comments.find(c => c.id === commentId);
                if (comment) {
                    // 如果已经点赞，取消点赞
                    if (comment.isLiked) {
                        comment.likeNum = Math.max(0, comment.likeNum - 1);
                        comment.isLiked = false;
                    }
                    // 切换点踩状态
                    if (!comment.isDisliked) {
                        comment.dislikeNum = (comment.dislikeNum || 0) + 1;
                        comment.isDisliked = true;
                    } else {
                        comment.dislikeNum = Math.max(0, (comment.dislikeNum || 0) - 1);
                        comment.isDisliked = false;
                    }
                }
            }
        },
        // 点赞回复
        likeReply(courseId: number, commentId: number, replyId: number) {
            const comments = this.comments[courseId];
            if (comments) {
                const comment = comments.find(c => c.id === commentId);
                if (comment && comment.replies) {
                    const reply = comment.replies.find(r => r.id === replyId);
                    if (reply) {
                        // 如果已经点踩，取消点踩
                        if (reply.isDisliked) {
                            reply.dislikeNum = Math.max(0, reply.dislikeNum - 1);
                            reply.isDisliked = false;
                        }
                        // 切换点赞状态
                        if (!reply.isLiked) {
                            reply.likeNum = (reply.likeNum || 0) + 1;
                            reply.isLiked = true;
                        } else {
                            reply.likeNum = Math.max(0, (reply.likeNum || 0) - 1);
                            reply.isLiked = false;
                        }
                    }
                }
            }
        },
        // 点踩回复
        dislikeReply(courseId: number, commentId: number, replyId: number) {
            const comments = this.comments[courseId];
            if (comments) {
                const comment = comments.find(c => c.id === commentId);
                if (comment && comment.replies) {
                    const reply = comment.replies.find(r => r.id === replyId);
                    if (reply) {
                        // 如果已经点赞，取消点赞
                        if (reply.isLiked) {
                            reply.likeNum = Math.max(0, reply.likeNum - 1);
                            reply.isLiked = false;
                        }
                        // 切换点踩状态
                        if (!reply.isDisliked) {
                            reply.dislikeNum = (reply.dislikeNum || 0) + 1;
                            reply.isDisliked = true;
                        } else {
                            reply.dislikeNum = Math.max(0, (reply.dislikeNum || 0) - 1);
                            reply.isDisliked = false;
                        }
                    }
                }
            }
        }
    },
    persist: {
        storage: localStorage,
        paths: ['comments']
    }
}); 